﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Drawing.Drawing2D;

namespace System.Windows.JControls.JColorPicker.Tracker
{
    /// <summary>
    /// A class the for the brightness tracker
    /// </summary>
    [System.ComponentModel.EditorBrowsable(System.ComponentModel.EditorBrowsableState.Never)]
    class HSBBrightnessTracker : ColorTracker
    {
        private Color _color;

        public HSBBrightnessTracker()
            : base()
        {
            SetValue(0, 100, 100);
        }

        public Color ColorTracker
        {
            get { return _color; }
            set
            {
                _color = value;
                Invalidate();
            }
        }

        private void SetColorValue(Point mousePoint)
        {
            Rectangle rect = RectangleUtils.RectanglePaint(ClientRectangle, Padding);
            double selectValue = 0;

            if (mousePoint.X < rect.X)
            {
                selectValue = 0;
            }
            else if (mousePoint.X > rect.Right)
            {
                selectValue = MaxValue;
            }
            else
            {
                selectValue = (mousePoint.X - rect.Left) * MaxValue / rect.Width;
            }

            if (selectValue > MaxValue)
            {
                selectValue = MaxValue;
            }

            Value = selectValue;
        }

        #region Override Methods

        protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
        {
            Rectangle rect = RectangleUtils.RectanglePaint(ClientRectangle, Padding);

            // Paints tghe color gradiant
            using (LinearGradientBrush brush = new LinearGradientBrush(rect, Color.FromArgb(0, 0, 0), _color, LinearGradientMode.Horizontal))
            {
                e.Graphics.FillRectangle(brush, rect);
            }

            // Paint the selctor
            PaintSelectors(e.Graphics, rect);
        }

        private void PaintSelectors(Graphics gr, Rectangle rectPaint)
        {
            GraphicsPath grpathUp = GetSignUpPath(rectPaint);

            using (SolidBrush brush = new SolidBrush(Color.Bisque))
            {
                gr.FillPath(brush, grpathUp);
            }

            using (Pen pen = new Pen(Color.Black))
            {
                gr.DrawPath(pen, grpathUp);
            }
        }

        protected override void OnMouseDown(System.Windows.Forms.MouseEventArgs e)
        {
            base.OnMouseDown(e);

            if (e.Button == System.Windows.Forms.MouseButtons.Left)
            {
                SetColorValue(e.Location);
            }
        }

        protected override void OnMouseMove(System.Windows.Forms.MouseEventArgs e)
        {
            base.OnMouseMove(e);

            if (e.Button == System.Windows.Forms.MouseButtons.Left)
            {
                SetColorValue(e.Location);
            }
        }

        #endregion
    }
}
